const { claimsModel } = require('./models/claimsModel.js');

//add
module.exports.addClaims = async claim => {
    return await claimsModel.create(claim);
}

//get
module.exports.getClaims = async () => {
    return await claimsModel.find();
}

//edit
module.exports.editClaims = async (_id, claim) => {
    console.log(_id);
    console.log(claim);
    return await claimsModel.updateOne({ _id }, claim);
}

//delete
module.exports.deleteClaims = async ({ _id }) => {
    console.log(_id);
    return await claimsModel.deleteOne({ _id });
}
//管理人员查看待处理的理赔
module.exports.getClaimApply = async (content) => {
    let { pageSize, currentPage,_id:value } = content;
    if(value==undefined||value==''){
        let totalSize = await claimsModel.countDocuments({ state: 1 })
        let data = await claimsModel.find({ state: 1 }).limit(pageSize - 0).skip((currentPage - 1) * (pageSize - 0));
        return { data, totalSize }
    }else{
        let totalSize = await claimsModel.countDocuments({$and:[{_id:value},{ state: 1 }]})
        let data=await claimsModel.find({$and:[{_id:value},{ state: 1 }]}).limit(pageSize - 0).skip((currentPage - 1) * (pageSize - 0))
        return { data, totalSize }
    }
}
//管理人员不同意理赔
module.exports.noClaimsApply = async (content) => {
    // console.log(content);
    return await claimsModel.updateOne(content, { state: 3 })
}
//管理人员同意理赔
module.exports.okClaimsApply = async (content) => {
    // console.log(content);
    return await claimsModel.updateOne(content, { state: 2 })
}
//管理查看已审核的理赔
module.exports.getFinishClaimApply = async (content) => {
    let { pageSize, currentPage, _id:value} = content;
    console.log( {_id:value});
    if(value==undefined||value==''){
        let totalSize = await claimsModel.countDocuments({ "$or": [{ state: 2 }, { state: 3 }] })
        let data = await claimsModel.find({ "$or": [{ state: 2 }, { state: 3 }] }).limit(pageSize - 0).skip((currentPage - 1) * (pageSize - 0));
        return { data, totalSize }
    }else{
        let totalSize = await claimsModel.countDocuments({$and:[{_id:value},{ "$or": [{ state: 2 }, { state: 3 }] }]})
        let data=await claimsModel.find({$and:[{_id:value},{ "$or": [{ state: 2 }, { state: 3 }] }]}).limit(pageSize - 0).skip((currentPage - 1) * (pageSize - 0))
        return { data, totalSize }
    }
    // 603aa0a8fea3584538beca21
}
